package cn.i7baozh.starter.client.controller;

import cn.i7baozh.starter.client.service.TestService;
import cn.i7baozh.starter.common.controller.GatewayRestClientApi;
import cn.i7baozh.starter.common.exception.BussinessRuntimeException;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Title:
 * @Description:
 * @author: baoqi.zhang
 * @date: 2022/5/8 18:00
 */
@Slf4j
@RefreshScope
@RestController
@RequestMapping("/client/api/test")
public class TestController {

    @Autowired
    private TestService testService;

    @GatewayRestClientApi
    @SentinelResource(value = "clientSayHello",blockHandler = "sayHelloBlockHandler")
    @GetMapping("/sayHello")
    public String sayHello(String name, HttpServletRequest request) {
        if (StringUtils.equals(name, "123")) {
            throw new BussinessRuntimeException(1222,"出错了思密达");
        }
        log.info("client sayHello param:{}", name);
        return testService.clientSayHello(name);
    }

    public String sayHelloBlockHandler(String name, BlockException blockException) {
        return "异常阻塞了";
    }

    @GatewayRestClientApi
    @GetMapping("/sayHi")
    public void sayHi(String name, HttpServletRequest request) throws BlockException{

        log.info("client sayHi param:{}", name);
        if (StringUtils.equals(name, "123")) {
            throw new BussinessRuntimeException(1222,"出错了思密达");
        }
        testService.clientSayHi(name);
    }
}
